package com.app.qbake.module.square;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.app.qbake.R;
import com.app.qbake.module.square.layout.SearchCategoryLayout;

/**
 * Created by wind on 2018/11/10.
 */

public class CategoryPopup extends PopupWindow {

    Activity mActivity;
    SearchCategoryLayout mSearchCategoryLayout;
    private  View anchor;

    public CategoryPopup(Activity activity,View anchor){
        RecyclerView rv= (RecyclerView) activity.getLayoutInflater().inflate(R.layout.pop_category,null);
        this.anchor=anchor;
        mSearchCategoryLayout=new SearchCategoryLayout(activity,rv);
        setContentView(rv);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        ColorDrawable white=new ColorDrawable(Color.WHITE);
        setBackgroundDrawable(white);
        mSearchCategoryLayout.setOnVisibilityChangeListener(new SearchCategoryLayout.OnVisibilityChangeListener() {
            @Override
            public void onVisibilityChange(int visibility) {
                switch (visibility){
                    case View.VISIBLE:
                        changeBg(0.5f);
                        showAsDropDown();
                        break;
                    case View.GONE:
                        dismiss();
                        break;
                }
            }
        });
        mActivity=activity;
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                changeBg(1.0f);
            }
        });


    }

    private void showAsDropDown() {
        showAsDropDown(anchor);
    }

    public void toggle(){

        mSearchCategoryLayout.toggle();
    }

    private void changeBg(float alpha){
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = alpha; //0.0-1.0
        mActivity.getWindow().setAttributes(lp);
    }


}
